Исходные данные задачи в стандартной форме
In [194]:
function get_problem()
A = Float64[3 2 1 1 0; 2 5 3 0 1]
b = Float64[10, 15]
c = Float64[-2, -3, -4, 0, 0]
A, b, c
end
Out[194]:
In [195]:
function update_vars(A, basis, nonbasis)
B = A[:,basis]
N = A[:,nonbasis]
B⁻¹ = inv(B)
b̂ = B⁻¹ * b
return B, B⁻¹, N, b̂
end
Out[195]:
In [196]:
function CHUZR(A, b̂)
m, n = size(A)
e = eye(m)
rations = [b̂[p] / norm(B⁻¹ * e[:,p]) for p in 1:m]
p = collect(take(sortperm(rations, rev=true), 1))
return p[1]
end
Out[196]:
In [197]:
function BTRAN(A, B⁻¹, p)
m, n = size(A)
e = eye(m)
πᵀ = e[:,p]' * B⁻¹
return πᵀ
end
Out[197]:
In [198]:
function PRICE(πᵀ, N)
âᵀ = πᵀ * N
return âᵀ
end
Out[198]:
In [199]:
function CHUZC(c, nonbasis, âᵀ)
ĉ = c[nonbasis]
rations = [ĉ[j] / âᵀ'[j] for j in 1:length(âᵀ)]
q = collect(take(sortperm(rations), 1))
ĉₙ = c[nonbasis]
β = ĉ[q] / âᵀ[q]
c[nonbasis] = (ĉₙ' - β * âᵀ)'
return q[1]
end
Out[199]:
In [200]:
function FTRAN(A, B⁻¹, b̂, p, âᵀ, q)
âq = B⁻¹ * A[:,q]
α = (b̂[p] / âᵀ[q])[1]
b̂ = b̂ - α * âq
end
Out[200]:
In [201]:
function update(basis, nonbasis, p, q)
basis[p], nonbasis[q] = nonbasis[q], basis[p]
end
Out[201]:
In [202]:
function iter(A, b, c, basis, nonbasis)
B, B⁻¹, N, b̂ = update_vars(A, basis, nonbasis)
p = CHUZR(A, b̂)
πᵀ = BTRAN(A, B⁻¹, p)
âᵀ = PRICE(πᵀ, N)
q = CHUZC(c, nonbasis, âᵀ)
FTRAN(A, B⁻¹, b̂, p, âᵀ, q)
update(basis, nonbasis, p, q)
end
Out[202]:
In [203]:
A, b, c = get_problem()
basis = [4, 5]
nonbasis = [1, 2, 3]
iter(A, b, c, basis, nonbasis)
Out[203]: